package com.example.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.Exception.ServiceException;
import com.example.springboot.Untils.TokenUtils;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * 雒鑫
 * Java Web
 */
@Service//通过该注解 注入到spring容器当中
public class UserService extends ServiceImpl<UserMapper, User> {
    @Resource
    UserMapper userMapper;


    @Override
    public boolean save(User entity) {
        if (StrUtil.isBlank(entity.getName())) {
            entity.setName(entity.getUsername());
        }
        if (StrUtil.isBlank(entity.getPassword())) {
            entity.setPassword("123");
        }
        if (StrUtil.isBlank(entity.getRole())) {
            entity.setRole("用户");
        }

        return super.save(entity);
    }

    //封装用户名查询方法
    public User selectByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);//eq => == where username=${username}
        return getOne(queryWrapper); //select *from user where username=${username}

    }

    //验证用户的账号是否合法
    public User login(User user) {
        /*QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());//eq => == where username=${username}
        User dbUser = userMapper.selectOne(queryWrapper);//  select *from user where username=${username}*/
        User dbUser = selectByUsername(user.getUsername());
        //根据用户名查询数据库的用户信息
        if (dbUser == null) {
            //抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        //生成token
        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    //注册页面
    public User register(User user) {
        User dbUser01 = selectByUsername(user.getUsername());
        if (dbUser01 != null) {
            //抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        user.setName(user.getUsername());
        userMapper.insert(user);
        return user;

    }

    //重置密码
    public void resetPassword(User user) {
        User dbuser = selectByUsername(user.getUsername());
        if (dbuser == null) {
            throw new ServiceException("用户不存在");
        }
        if (!user.getPhone().equals(dbuser.getPhone())) {
            throw new ServiceException("验证错误");
        }
        dbuser.setPassword("123");
        updateById(dbuser);
    }

    /*  public void insertUser(User user) {
     *//*
        逻辑:这个user对象来自与controller当中
        controller当中的user对象来自于前端
         *//*
     *//*
        从前端将user传递到controller 在从controller传递到service
        从service传递到manper 在mapper中执行sql 存入到数据库
         *//*
        userMapper.insert(user);

    }

    public void updateUser(User user) {
        userMapper.updataUser(user);
    }

    *//*
    单个用户信息删除
     *//*
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    *//*
    批量删除多个用户信息
     *//*
    public void BatchDeleteUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);
        }
    }

    *//*
    查询全部用户信息
     *//*
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    *//*
    根据id查询当前用户信息
     *//*
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    *//*
    根据姓名进行多条件查询（返回的结果不再是一个对象）
     *//*
    public List<User> selectByname(String name) {
        return userMapper.selectByname(name);
    }

    *//*
    多条件查询
     *//*
    public User SelectMore(String username, String name) {
        return userMapper.SelectMore(username, name);
    }

    *//*
    多条件模糊查询
     *//*
    public List<User> SeletcByMo(String username, String name) {
        return userMapper.SeletcByMo(username, name);
    }

    *//*
多条件模糊分页查询
 *//*
    public Page<User> SeletcByPage(Integer pageNumber, Integer pageSize, String username, String name) {
        Integer skipNum = (pageNumber - 1) * pageSize;//要计算skipNum
        //定义一个Page对象 这个对象封装查询到显示的用户的列表 以及当前用户的总数
        *//*
         private  Integer tatol;
         List<T> list;
         *//*
        Page<User> userPage = new Page<>();
        List<User> userList = userMapper.SeletcByPage(skipNum, pageSize, username, name);
        Integer total = userMapper.SeletcCountByPage(username, name);
        //1.将查询到的用户列表封装到page类当中
        userPage.setList(userList);
        //2.将查询到的用户总数封装到page类当中
        userPage.setTatol(total);
        //3.返回会这个page对象
        return userPage;

    }*/

}
